package com.sy.service.report.impl;

import com.alibaba.excel.EasyExcel;
import com.sy.bean.pucharse.excel.PurchaseExcel;
import com.sy.bean.report.PurchaseReport;
import com.sy.bean.report.PurchaseReportExcel;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.report.PurchaseReportMapper;
import com.sy.service.report.PurchaseReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseReportServiceImpl extends BaseServiceImpl<PurchaseReport, PurchaseReportMapper> implements PurchaseReportService {

    @Autowired
    PurchaseReportMapper purchaseReportMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public List<Map> querySupplierName(int eid) {
        return purchaseReportMapper.querySupplierName(eid);
    }

    @Override
    public List<Map> queryWarehouse(int eid) {
        return purchaseReportMapper.queryWarehouse(eid);
    }

    @Override
    public String exportPurchaseReport(Map map) {
        List<PurchaseReportExcel> list = purchaseReportMapper.exportPurchaseReport(map);
        String root = request.getServletContext().getRealPath("/") + "/excel";
        File file = new File(root);
        if (!file.exists())
            file.mkdirs();
        String newFileName = file.getAbsolutePath() + "/采购报表列表.xlsx";
        EasyExcel.write(newFileName, PurchaseReportExcel.class)
                .sheet("采购报表列表")
                .doWrite(() -> {
                    return list;
                });
        return "/excel/采购报表列表.xlsx";
    }
}
